import { DataTypes, Model, Sequelize } from 'sequelize';

export interface TemplateAttributes {
  id?: number;
  name: string;
  category: string;
  framework?: 'react' | 'vue' | 'angular';
  description?: string;
  template_code: string;
  dependencies?: string[] | null;
  popularity_score?: number;
  created_at?: Date;
  updated_at?: Date;
}

export interface TemplateCreationAttributes extends Omit<TemplateAttributes, 'id' | 'created_at' | 'updated_at'> {}

export class Template extends Model<TemplateAttributes, TemplateCreationAttributes> implements TemplateAttributes {
  public id!: number;
  public name!: string;
  public category!: string;
  public framework?: 'react' | 'vue' | 'angular';
  public description?: string;
  public template_code!: string;
  public dependencies?: string[] | null;
  public popularity_score?: number;
  
  // timestamps!
  public readonly created_at!: Date;
  public readonly updated_at!: Date;

  static initModel(sequelize: Sequelize): typeof Template {
    return Template.init(
      {
        id: {
          type: DataTypes.INTEGER,
          primaryKey: true,
          autoIncrement: true,
        },
        name: {
          type: DataTypes.STRING(100),
          allowNull: false,
          unique: true,
        },
        category: {
          type: DataTypes.STRING(50),
          allowNull: false,
        },
        framework: {
          type: DataTypes.ENUM('react', 'vue', 'angular'),
          defaultValue: 'react',
        },
        description: {
          type: DataTypes.TEXT,
          allowNull: true,
        },
        template_code: {
          type: DataTypes.TEXT('long'),
          allowNull: false,
        },
        dependencies: {
          type: DataTypes.JSON,
          allowNull: true,
        },
        popularity_score: {
          type: DataTypes.INTEGER,
          defaultValue: 0,
        },
      },
      {
        sequelize,
        tableName: 'templates',
        timestamps: true,
        createdAt: 'created_at',
        updatedAt: 'updated_at',
        indexes: [
          {
            fields: ['category', 'framework'],
          },
          {
            fields: ['popularity_score'],
          },
        ],
      }
    );
  }
}

export interface ComponentCacheAttributes {
  cache_key: string;
  cache_data: any;
  cache_type: 'search' | 'component' | 'template';
  expires_at: Date;
  created_at?: Date;
}

export interface ComponentCacheCreationAttributes extends Omit<ComponentCacheAttributes, 'created_at'> {}

export class ComponentCache extends Model<ComponentCacheAttributes, ComponentCacheCreationAttributes> implements ComponentCacheAttributes {
  public cache_key!: string;
  public cache_data!: any;
  public cache_type!: 'search' | 'component' | 'template';
  public expires_at!: Date;
  
  public readonly created_at!: Date;

  static initModel(sequelize: Sequelize): typeof ComponentCache {
    return ComponentCache.init(
      {
        cache_key: {
          type: DataTypes.STRING(255),
          primaryKey: true,
        },
        cache_data: {
          type: DataTypes.JSON,
          allowNull: false,
        },
        cache_type: {
          type: DataTypes.ENUM('search', 'component', 'template'),
          allowNull: false,
        },
        expires_at: {
          type: DataTypes.DATE,
          allowNull: false,
        },
      },
      {
        sequelize,
        tableName: 'component_cache',
        timestamps: true,
        createdAt: 'created_at',
        updatedAt: false,
        indexes: [
          {
            fields: ['cache_type', 'expires_at'],
          },
        ],
      }
    );
  }
}

export interface PerformanceMetricAttributes {
  id?: number;
  operation_type: string;
  execution_time_ms: number;
  cache_hit?: boolean;
  timestamp?: Date;
}

export interface PerformanceMetricCreationAttributes extends Omit<PerformanceMetricAttributes, 'id' | 'timestamp'> {}

export class PerformanceMetric extends Model<PerformanceMetricAttributes, PerformanceMetricCreationAttributes> implements PerformanceMetricAttributes {
  public id!: number;
  public operation_type!: string;
  public execution_time_ms!: number;
  public cache_hit?: boolean;
  
  public readonly timestamp!: Date;

  static initModel(sequelize: Sequelize): typeof PerformanceMetric {
    return PerformanceMetric.init(
      {
        id: {
          type: DataTypes.INTEGER,
          primaryKey: true,
          autoIncrement: true,
        },
        operation_type: {
          type: DataTypes.STRING(50),
          allowNull: false,
        },
        execution_time_ms: {
          type: DataTypes.INTEGER,
          allowNull: false,
        },
        cache_hit: {
          type: DataTypes.BOOLEAN,
          defaultValue: false,
        },
      },
      {
        sequelize,
        tableName: 'performance_metrics',
        timestamps: true,
        createdAt: 'timestamp',
        updatedAt: false,
        indexes: [
          {
            fields: ['operation_type', 'timestamp'],
          },
        ],
      }
    );
  }
}